package com.xiezc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.cache.impl.TimedCache;
import com.xiezc.dao.UserRepository;
import com.xiezc.dto.ExcepEnum;
import com.xiezc.dto.RespData;
import com.xiezc.entity.User;
import com.xiezc.util.XUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * Created by Administrator on 2017/5/19.
 */
@RestController
public class LoginAction {

    @Resource
    private UserRepository userRepository;

    @Resource
    private TimedCache timedCache;

    /**
     * 用户注册
     *
     * @param userDTO
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public RespData register(@RequestBody User userDTO) {
        Assert.notNull(userDTO, "请传入正确的参数");
        Assert.notNull(userDTO.getUserName(), "请传入正确的参数");
        Assert.notNull(userDTO.getPassword(), "请传入正确的参数");
        if (userDTO.getUserName().length() > 16 || userDTO.getUserName().length() < 5) {
            Assert.notNull(null, "用户名的长度只能在5到16之间");
        }
        if (userDTO.getPassword().length() > 16 || userDTO.getUserName().length() < 5) {
            Assert.notNull(null, "密码的长度只能在5到16之间");
        }
        //查看用户名是否有重复的 
        User user = userRepository.findByUserName(userDTO.getUserName());
        if (Objects.isNull(user)) {
            //新增用户
            // BeanUtils.copyProperties(userDTO, user);
            userRepository.save(userDTO);
            userDTO.setPassword(null);
            return RespData.success(userDTO);
        }

        return RespData.fail(ExcepEnum.UsedUName);
    }


    /**
     * 用户登录
     *
     * @param userDTO
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public RespData login(@RequestBody User userDTO) {
        Assert.notNull(userDTO, "请传入正确的参数");
        Assert.notNull(userDTO.getUserName(), "请传入正确的参数");
        Assert.notNull(userDTO.getPassword(), "请传入正确的参数");

        User user1 = userRepository.findByUserName(userDTO.getUserName());

        if (Objects.nonNull(user1) && Objects.equals(user1.getPassword(), userDTO.getPassword())) {
            String ticket = XUtil.getUUID();
            timedCache.put(ticket, user1);
            user1.setPassword(null);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ticket", ticket);
            jsonObject.put("user", user1);
            return RespData.success(jsonObject);
        }
        return RespData.fail(ExcepEnum.UNameOrPwdWrong);
    }


    /**
     * 用户退出
     *
     * @param ticket
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public RespData logout(String ticket) {
        Assert.notNull(ticket, "参数不能为空");
        timedCache.remove(ticket);
        return RespData.fail(ExcepEnum.UNameOrPwdWrong);
    }


}
